<template>
	<view class="order-detail">
		<view class="w96 radius20 bgwhite mt20 look-card" v-if="current==0||current==1">
			<text class="text">{{detail.look_num}}</text><text>位师傅正在查看</text>
		<!-- 	<view class="mt20 small-size">
				距离订单截止时间:51小时11分55秒
			</view> -->
		</view>
		<view class="w96 radius20 bgwhite mt20 look-card small-size" v-if="current==2">
			<text class="text">{{detail.look_num||1}}</text><text>位师傅正在查看</text>
			<!-- 	<view class="mt20 small-size">
				<text>只剩</text> <text class="text">{{detail.look_num||8}}</text><text>个报价名额，尽快去报价</text>
			</view> -->
		</view>
		<view class="w96 radius20 bgwhite mt20 look-card" v-if="current==3">
			<text>待接单 </text>
			<view class="mt20 small-size">
				距离订单截止时间:51小时11分55秒
			</view>
		</view>
		<view class="w96 radius20 bgwhite mt20">
			<view class="w94 peo-text" style="">
				客户信息
			</view>
			<view class="flex phone" style="">
				<image src="@/static/order/user.png" class="img" style="" mode="">
				</image>
				<text class="small-size">{{ detail.user && detail.user.mobile+'(转虚拟号码)' || '手机号(转虚拟号码)'}}</text>
			</view>
			<view class="flex phone" style="">
				<image src="@/static/order/dw.png" class="img" mode="">
				</image>
				<text
					style="font-weight: bold;font-size: 24rpx;">{{ detail.province+ detail.city+ detail.area + detail.address ||'用户的具体居住地址'}}</text>
			</view>
			<view class="" style="padding-left: 100rpx;">
				<view class="buttons" style=""
					@click="copy(detail.province+ detail.city+ detail.area + detail.address)">
					复制
				</view>
				<view class=" align-24" style="">
					距您当前直线距离约{{(Number(detail.distance)/1000).toFixed(2) ||0}}km
				</view>
			</view>
			<view class="flex phone" style="">
				<image src="@/static/order/time.png" class="img" mode="">
				</image>
				<text class="small-size">期望上门时间:{{detail.datetime||"接单后与业主预约"}}</text>
			</view>
		</view>

		<view class="w96 radius20 bgwhite mt20" style="padding: 0 20rpx 20rpx 0rpx;">
			<view class="w94 peo-text" style="">
				商品信息
			</view>
			<view class="flex" style="padding: 20rpx;">
				<image :src="detail.goods_photo.split(',')[0]" v-if="detail.goods_photo" class="goodsImg" style="" mode="" />
				<image src="@/static/lg.png" v-else class="goodsImg" style="" mode="" />
				<view class="" style="margin-top: 40rpx;">
					<text class="goodsText" style="">{{detail.goods_name||"橱柜"}}</text>
					<view class="goodsText" style="">商品数量:{{detail.goods_number||1}}台</view>
				</view>
			</view>
			<view class="flex" style="padding-left: 40rpx;">
				<text style="font-size: 26rpx;line-height: 2;">服务类型:</text>
				<view class="buttons" style="">
					{{detail.category_name||'维修'}}
				</view>
			</view>
			<view class="" style="padding-left: 40rpx;">
				<text style="font-size: 26rpx;">商品备注: {{detail.goods_remark||'无'}}</text>
			</view>

		</view>
		<view class="w96 radius20 bgwhite mt20">
			<view class="w94 peo-text" style="">
				订单信息
			</view>
			<view class="w94" style="padding: 0 0 30rpx 20rpx;">
				<view class="flex-between mt20">
					<view class="order-text" style="">
						订单号码:{{detail.order_sn}}
					</view>
					<view class="buttons" style="" @click="copy(detail.order_sn)">
						复制
					</view>
				</view>
				<view class="order-text" style="">
					订单备注:{{detail.remark||"请携带工具上门"}}
				</view>
				<view class="order-text" style="">
					下单时间:{{format(detail.createtime*1000)}}
				</view>
			</view>
		</view>
		<view class="w96 radius20 bgwhite mt20" v-if="current!=2">
			<view class="w94" style="padding:30rpx 40rpx;border-bottom:2rpx solid #eee;">
				合计
			</view>
			<view class="w94" style="padding-left: 40rpx;">
				<view class="order-text" style="">
					{{detail.category_name||'维修'}}
				</view>
				<view class="flex" style="line-height:2;justify-content: flex-end;align-items: center;">
					<text style="font-size: 24rpx;">到手合计:</text> <text style="color: red;font-size: 24rpx;">￥</text>
					<text style="color: red;font-size: 34rpx;">{{detail.total_money}}</text>
				</view>
			</view>
		</view>
		<view class="bot-button-bg" style="" v-if="current==0">
			<button open-type="share"
				style="color:#000;border: none;border-color:transparent;width: 20%;font-size: 24rpx;" plain>
				<u-icon name="/static/order/share.png" label="转发给朋友" labelPos="bottom" labelSize="12px">
				</u-icon>
			</button>
			<view class="button" style="" v-if="detail.is_baoming==0" @click="accept(detail.id)">
				立即报名
			</view>
			<view class="button dis" style="" v-else>
				已报名
			</view>
		</view>
		<view class="bot-button-bg" style="" v-if="current==1">
			<button open-type="share"
				style="color:#000;border: none;border-color:transparent;width: 20%;font-size: 24rpx;" plain>
				<u-icon name="/static/order/share.png" label="转发给朋友" labelPos="bottom" labelSize="12px">
				</u-icon>
			</button>
			<view class="button" style="" v-if="detail.is_baojia==0" @click="accept(detail.id)">
				立即抢单
			</view>
			<view class="button dis" style="" v-else>
				已抢单
			</view>
		</view>
		<view class="bot-button-bg" style="" v-if="current==2">
			<button open-type="share"
				style="color:#000;border: none;border-color:transparent;width: 20%;font-size: 24rpx;" plain>
				<u-icon name="/static/order/share.png" label="转发给朋友" labelPos="bottom" labelSize="12px">
				</u-icon>
			</button>
			<view class="button" style="" v-if="detail.is_baojia==0" @click="accept(detail.id)">
				立即报价
			</view>
			<view class="button dis" style="" v-else>
				已报价
			</view>
		</view>
		<view class="bot-button-bg" style="" v-if="current==3">
			<view class="button w50 dis" style="" @click="show=true">
				拒绝接单
			</view>
			<view class="button  w50" style="" @click="accept(detail.id)">
				确认接单
			</view>
		</view>
		<u-popup :show="show" @close="show=false">
			<view>
				<view class="" style="text-align: center;font-size: 28rpx;font-weight: bold;padding: 20rpx 0;">
					请说明拒绝接受指派订单原因
				</view>
				<view class="" style="padding: 15rpx;">

					<u--textarea v-model="note" placeholder="请说明拒绝理由!" :height="290"></u--textarea>
				</view>
				<view class="bot-button-bg" style="box-shadow: none;">
					<view class="button" style="width: 100%;" @click="refuse(detail.id)">
						立即提交
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		orderDetail,
		order_accept,
		orderLocal,
		confirm_order,
		refuse_order
	} from '@/common/teacherReq';
	export default {
		data() {
			return {
				current: 0,
				detail: {},
				show: false,
				note: ''
			}
		},
		methods: {
			async getDetail(id) {
				try {
					let {
						longitude,
						latitude
					} = await wx.getLocation({
						type: 'wgs84'
					})
					let {
						data
					} = await orderDetail({
						id,
						// lat: 116.45623,
						// lng: 35.146521
						lat: latitude || '',
						lng: longitude || ''
					})
					this.detail = data.order_info
				} catch (error) {
					//TODO handle the exception
					let {
						data
					} = await orderDetail({
						id,
						// lat: 116.45623,
						// lng: 35.146521
					})
					this.detail = data.order_info
				}

			},
			format(timestamp) {
				const date = new Date(timestamp);
				const year = date.getFullYear();
				const month = date.getMonth() + 1; // 月份从0开始，所以要加1
				const day = date.getDate();
				const hours = date.getHours();
				const minutes = date.getMinutes();
				const formattedMinutes = minutes < 10 ? `0${minutes}` : minutes;
				const formattedDate = `${year}年${month}月${day}日   ${hours}:${formattedMinutes}`;
				return formattedDate

			},
			accept(id) {
				const actions = {
					0: () => order_accept({
						order_id: id
					}),
					1: () => orderLocal({
						order_id: id
					}),
					2: () => new Promise((resolve, reject) => {
						this.toPage('/subpack/index/offer?type=' + 2 + '&id=' + id);
						resolve();
					}),
					3: () => confirm_order({
						order_id: id
					}),
					default: () => {
						uni.showToast({
							title: '无效的操作',
							icon: 'none'
						});
						return Promise.reject(new Error('无效的操作'));
					}
				};

				const action = actions[Number(this.current)] || actions.default;
				action()
					.then(res => {
						uni.showToast({
							title: '成功',
							icon: 'success'
						});
						if (this.current != 2) {
							// 所有操作完成后跳转到首页
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/index/index'
								});
							}, 1500)
						}
					})
					.catch(error => {
						console.error('操作失败:', error);
					});
			},
			refuse(id) {
				if (!this.note.length) {
					uni.showToast({
						title: '拒绝理由不能为空',
						icon: 'error'
					})
					return
				}
				refuse_order({
					order_id: id,
					content: this.note
				}).then(res => {
					uni.showToast({
						title: res.msg,
						icon: 'error'
					})
					this.show = false
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/index/index'
						})
					}, 1500)
				})
			},
			copy(e) {
				uni.setClipboardData({
					data: e
				})
			},
			share() {}
		},
		onLoad(opt) {
			this.getDetail(opt.id)
			this.current = opt.type
		}
	}
</script>

<style scoped lang="scss">
	.order-detail {
		position: relative;

		.look-card {
			padding: 40rpx;

			.text {
				color: $uni-color-primary; //#96ee53
			}

			.small-size {
				color: #cbcbcb;
			}
		}

		.peo-text {
			padding: 30rpx 40rpx;
			border-bottom: 2rpx solid #eee;
		}

		.phone {
			padding: 20rpx 0 20rpx 40rpx;
			align-items: center;

			.img {
				width: 40rpx;
				height: 40rpx;
				padding-right: 20rpx
			}
		}

		.buttons {
			font-size: 12px;
			max-width: 14%;
			padding: 5px;
			text-align: center;
			border: 1px solid #eee;
		}

		.align-24 {
			align-items: center;
			font-size: 24rpx;
		}

		.goodsImg {
			width: 140rpx;
			height: 140rpx;
			margin: 20rpx;
			border: 1px solid;
			border-radius: 16rpx;
		}

		.goodsText {
			font-size: 28rpx;
			margin-top: 40rpx;
		}

		.order-text {
			line-height: 2;
			font-size: 26rpx;
		}

		.bot-button-bg {
			box-shadow: 1px -5px 10px #ccc;
			position: sticky;
			bottom: 0;
			padding: 30rpx;
			background-color: #fff;
			display: flex;
			justify-content: space-between;

			.button {
				width: 80%;
				height: 90rpx;
				border-radius: 90rpx;
				background-color: #CEEE5B;
				text-align: center;
				line-height: 2.5;
			}

			.w50 {
				width: 45%;
			}

			.dis {
				background-color: #f3f3f3;
			}
		}
	}
</style>